<center>
  <h2><%=h Tag.find_by_id(params[:id].to_i).name %></h2>
  <h4><%=h Tag.find_by_id(params[:id].to_i).description %>
  <h5>Data from <%=h params[:series]%> - 

  <% if params[:year] && params[:month] %>
    <% @url = "./#{params[:id]}?year=#{params[:year]}&month=#{params[:month]}&series=#{params[:series]}&col=#{params[:col]}&category=#{params[:category]}" %>
    <% @f_sql = "ORDER BY upload_day ASC" %>
    <% @s_sql = "ORDER BY upload_day ASC" %>
    <% @t_sql = "ORDER BY upload_day ASC" %>
    <% @y_sql = "ORDER BY upload_day ASC" %>

    <% if params[:category].length == 3 %>
      <% @year = params[:year].to_i %>
      <% @year = @year + 1 unless (params[:month].to_i + params[:col].to_i) < 13 %>
      <% @sql = "WHERE tag_id=#{params[:id]} AND upload_year=#{@year} AND upload_month=#{Date::ABBR_MONTHNAMES.index(params[:category])} " %>
      <a href=<%=h ActionController::Base.relative_url_root%>/data/month_csv/<%=h params[:id] %>?site=<%=h params[:series].downcase %>&y=<%=h @year %>&m=<%=h Date::ABBR_MONTHNAMES.index(params[:category]) %>>Export to CSV</a></h5>
    <% else %>
      <a href=<%=h ActionController::Base.relative_url_root%>/data/day_csv/<%=h params[:id] %>?site=<%=h params[:series].downcase %>&y=<%=h params[:year] %>&m=<%=h params[:month] %>&d=<%=h params[:col] %>>Export to CSV</a></h5>
      <% @sql = "WHERE tag_id=#{params[:id]} AND upload_year=#{params[:year]} AND upload_month=#{params[:month]} AND upload_day=#{params[:category]} " %>
    <% end %>
  <% else %>
    <% @url = "./#{params[:id]}?&series=#{params[:series]}&col=#{params[:col]}&category=#{params[:category]}" %>
    <% @f_sql = "ORDER BY upload_day ASC, upload_month ASC, upload_year ASC" %>
    <% @s_sql = "ORDER BY upload_day ASC, upload_month ASC, upload_year ASC" %>
    <% @t_sql = "ORDER BY upload_day ASC, upload_month ASC, upload_year ASC" %>
    <% @y_sql = "ORDER BY upload_day ASC, upload_month ASC, upload_year ASC" %>
    <% @sql = "WHERE tag_id=#{params[:id]} " %>
    <a href=<%=h ActionController::Base.relative_url_root%>/data/all_csv/<%=h params[:id] %>?site=<%=h params[:series].downcase %>>Export to CSV</a></h5>
  <% end %>

  <% if params[:sort] %>
      <% @f_sql = "ORDER BY owner ASC" %>
      <% @s_sql = "ORDER BY username ASC" %>
      <% @t_sql = "ORDER BY in_reply_to_screen_name ASC" %>
      <% @y_sql = "ORDER BY author ASC" %>
  <% end %>
  
  
</center>
  <table>
    <tr>
      <th>Title</th>
      <th>Description</th>
      <th><a title="Sort by user" href=<%=h @url %>&sort=user>User</a></th>
      <th><a title="Sort by date" href=<%=h @url %>>Date</a></th>
    </tr>
    <% if params[:series] == "Flickr"%>
        <% Flickr.find_by_sql("SELECT * FROM flickrs #{@sql}#{@f_sql}").each do |flickr| %>
          <tr>
            <td><a href=http://www.flickr.com/photos/<%=h flickr.owner%>/<%=h flickr.photoid%>><%=h flickr.title %></a></td>
            <td><%=h flickr.tags %></td>
            <td><a href=http://www.flickr.com/photos/<%=h flickr.owner%>/><%=h flickr.owner %></a></td>
            <td><%=h flickr.upload_day %>/<%=h flickr.upload_month %>/<%=h flickr.upload_year %></td>
          </tr>
        <% end %>
    <% end %>
    <% if params[:series] == "Slideshare"%>
        <% Slideshare.find_by_sql("SELECT * FROM slideshares #{@sql}#{@s_sql}").each do |slideshare| %>
          <tr>
            <td><a href=<%=h slideshare.url%>><%=h slideshare.title %></a></td>
            <td><%=h slideshare.description %></td>
            <td><a href=http://www.slideshare.net/<%=h slideshare.username %>/><%=h slideshare.username %></a></td>
            <td><%=h slideshare.upload_day %>/<%=h slideshare.upload_month %>/<%=h slideshare.upload_year %></td>
          </tr>
        <% end %>
    <% end %>
    <% if params[:series] == "Twitter"%>
        <% Tweet.find_by_sql("SELECT * FROM tweets #{@sql}#{@t_sql}").each do |tweet| %>
          <tr>
            <td><a href=http://twitter.com/<%=h tweet.in_reply_to_screen_name %>/statuses/<%=h tweet.twitter_status_id %>><%=h tweet.twitter_status_id %></a></td>
            <td><%=h tweet.text %></td>
            <td><a href=http://twitter.com/<%=h tweet.in_reply_to_screen_name %>/><%=h tweet.in_reply_to_screen_name %></a></td>
            <td><%=h tweet.upload_day %>/<%=h tweet.upload_month %>/<%=h tweet.upload_year %></td>
          </tr>
        <% end %>
    <% end %>
    <% if params[:series] == "Youtube"%>
        <% Youtube.find_by_sql("SELECT * FROM youtubes #{@sql}#{@y_sql}").each do |youtube| %>
          <tr>
            <td><a href=<%=h youtube.player_url %>><%=h youtube.title %></a></td>
            <td><%=h youtube.description %></td>
            <td><a href=http://www.youtube.com/<%=h youtube.author %>/><%=h youtube.author %></a></td>
            <td><%=h youtube.upload_day %>/<%=h youtube.upload_month %>/<%=h youtube.upload_year %></td>
          </tr>
        <% end %>
    <% end %>
</table>




